<template>
  <!-- 登录页面 路由组件 -->
  <div class="back-login">
    <div class="login">
      <div class="form">
        <div class="form-item login-title" title="遇见音乐2.0">遇见音乐2.0</div>
        <div class="form-item">
          <label for="">用户名</label>
          <input type="text" v-model="username" ref="username" />
        </div>
        <div class="form-item">
          <label for="">密码</label>
          <input :type="type" v-model="password" />
          <template v-if="password">
            <img
              v-show="type === 'password'"
              class="eyes"
              src="../../assets/svg/eye_no.svg"
              alt=""
              height="20"
              width="20"
              @click="eyeClickHandler('text')"
            />
            <img
              v-show="type === 'text'"
              class="eyes"
              src="../../assets/svg/eye_yes.svg"
              alt=""
              height="20"
              width="20"
              @click="eyeClickHandler('password')"
            />
          </template>
        </div>
        <div class="form-item submit" @click="login">登录</div>
      </div>
    </div>
  </div>
</template>

<script>
import { nanoid } from "nanoid";
import { mapActions } from "vuex";
export default {
  name: "BackLogin",
  data() {
    return {
      username: "admin",
      password: "admin",
      type: "password",
    };
  },
  methods: {
    /* 加载 更新用户信息 更新用户token 的函数 */
    ...mapActions("user", ["updateUserInfo", "updateToken"]),
    /* 显示隐藏/密码 */
    eyeClickHandler(type) {
      this.type = type;
    },
    /* 模拟登录 */
    login() {
      if (this.username === "admin" && this.password === "admin") {
        /* 加载一下 */
        const loading = this.$loading({
          lock: true,
          text: "登录中...",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)",
        });
        /* 帅一手 */
        setTimeout(() => {
          /* 写入用户数据 */
          const userinfo = {
            name: "特邀用户",
            avatarURL: "https://i.postimg.cc/Hk1cYSHy/avatar-default.jpg",
            username: "test",
            isvip: 0,
            opentime: null,
            losetime: null,
            popularity: "1亿",
            integral: "0",
            fans: "6666万",
            focus: "0",
          };
          /* 获取用户令牌 */
          const token = nanoid();
          this.updateUserInfo(userinfo);
          this.updateToken(token);
          loading.close();
          this.$router.replace({ path: "/mine" });
        }, 2000);
      } else {
        this.$message({ message: "账号或密码错误", type: "error" });
      }
    },
  },
  mounted() {
    const username = this.$refs["username"];
    username.focus();
  },
};
</script>

<style lang="less" scoped>
@keyframes spotlight {
  0% {
    clip-path: ellipse(32px 32px at 0% 50%);
  }
  100% {
    clip-path: ellipse(32px 32px at 100% 50%);
  }
}
.back-login {
  height: 100vh;

  .login {
    box-sizing: border-box;
    width: 8rem;
    height: 6.5rem;
    background-color: #333;
    margin: 2.5rem auto;
    padding: 0.5rem 1rem;
    border-radius: 0.2rem;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
    .form {
      margin: 0.5rem auto;
      margin-top: 0rem;
      .login-title {
        display: inline-block !important;
        font-size: 0.5rem;
        position: relative;
        font-weight: bold;
        color: #57be84;

        &::after {
          content: attr(title);
          position: absolute;
          height: 100%;
          width: 100%;
          top: 0;
          left: 0;
          color: #fff;
          animation: spotlight 3s ease-in-out infinite alternate;
        }
      }
      .form-item {
        position: relative;
        display: flex;
        flex-direction: column;
        margin-bottom: 0.3rem;
        .eyes {
          position: absolute;
          right: 0.1rem;
          top: 0.7rem;
          cursor: pointer;
        }
        &.submit {
          align-items: center;
          justify-content: center;
          margin-top: 0.6rem;
          height: 0.7rem;
          cursor: pointer;
          background-color: #57be84;
          color: #333;
          border-radius: 0.05rem;
          &:hover {
            background-color: #57be84e7;
            color: #fff;
          }
        }
        label {
          margin-bottom: 0.2rem;
        }
        input {
          height: 0.7rem;
          outline: none;
          border: 0;
          padding: 0.05rem 0;
          padding-left: 0.1rem;
          background-color: rgba(103, 103, 103, 0.5);
          color: #fff;
          padding: 0.1rem;
          padding-left: 0.2rem;
          border-radius: 0.05rem;
          &:focus {
            background-color: rgba(103, 103, 103, 0.8);
          }
          padding-right: 0.6rem;
        }
      }
    }
  }
}
</style>